package net.slavics.images;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.URL;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;

/**
 *
 * @author slavic01
 * @version $Revision: 19 $
 * $LastChangedDate: 2008-04-30 08:42:28 +0000 (Wed, 30 Apr 2008) $ $Author: pslavicek@seznam.cz $
 */
public class Images {

    private static Logger _log = Logger.getLogger(Images.class);
    private static Font font = new Font("Tahoma", Font.PLAIN, 10);

    /**
     * Vrátí prázdný testovací obrázek 256 x 256
     * @param width 
     * @param height 
     * @return Obrázek
     */
    public static BufferedImage getTestImage(int width, int height) {
        BufferedImage im = null;

        im = new BufferedImage(256, 256, BufferedImage.TYPE_INT_RGB);
        Graphics g = im.getGraphics();
        g.setColor(Color.white);
        g.fillRect(0, 0, width, height);

        g.setColor(Color.blue);
        int a0 = 0;
        int a1 = width / 4 - 1;
        int a2 = width / 2 - 1;
        int a3 = width / 4 * 3 - 1;
        int a4 = width - 1;
        g.drawLine(a1, 0, a2, 0);
        g.drawLine(a3, 0, a4, 0);
        g.drawLine(a4, a1, a4, a2);
        g.drawLine(a4, a3, a4, a4);
        int b0 = 0;
        int b1 = width / 4 - 1;
        int b2 = width / 2 - 1;
        int b3 = width / 4 * 3 - 1;
        int b4 = width - 1;
        g.drawLine(b0, b4, b1, b4);
        g.drawLine(b2, b4, b3, b4);
        g.drawLine(0, b0, 0, b1);
        g.drawLine(0, b2, 0, b3);

        g.setColor(Color.red);
        int okraj = 2;
        g.drawRect(okraj, okraj, width - 1 - 2 * okraj, height - 1 - 2 * okraj);
        g.dispose();

        return im;
    }

    /**
     * Vloží texty do obrázku
     * @param image
     * @param texty
     */
    public static void setText(BufferedImage image, String[] texty) {
        Graphics g = image.getGraphics();
        g.setFont(font);
        for (int i = 0; i < texty.length; i++) {
            g.setColor(Color.WHITE);
            g.drawString(texty[i], 9, 49 + i * 20);
            g.setColor(Color.BLACK);
            g.drawString(texty[i], 10, 50 + i * 20);
        }
        g.dispose();
    }

    /**
     * Vloží název do obrázku
     * @param image
     * @param name
     */
    public static void setName(BufferedImage image, String name) {
        Graphics g = image.getGraphics();
        g.setFont(font);
        g.setColor(Color.WHITE);
        g.drawString(name, 9, 14);
        g.setColor(Color.BLUE);
        g.drawString(name, 10, 15);
        g.dispose();
    }

    /**
     * Vloží chybu do obrázku
     * @param image
     * @param chyba
     */
    public static void setErrText(BufferedImage image, String chyba) {
        Graphics g = image.getGraphics();
        g.setFont(font);
        g.setColor(Color.PINK);
        g.drawString(chyba, 9, image.getHeight() - 16);
        g.setColor(Color.RED);
        g.drawString(chyba, 10, image.getHeight() - 15);
        g.dispose();
    }

    /**
     * Načte obrázek ze zadané adresy
     * <br/><code>
     * java.net.Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("cache-bak.pre.cz", 8080))
     * </code>
     * @param url Adresa obrázku
     * @param proxy Objekt proxy, když je NULL jde přímo
     * @return Obrázek
     * @throws java.io.IOException
     */
    public static BufferedImage getImage(URL url, Proxy proxy) throws IOException {
        BufferedImage im = null;
        HttpURLConnection con;
        _log.debug("Connect to " + url.getHost());
        if (proxy == null) {
            con = (HttpURLConnection) url.openConnection();
        } else {
            _log.debug("Proxy: " + proxy);
            con = (HttpURLConnection) url.openConnection(proxy);
        }
        _log.debug("Response code: " + con.getResponseCode() + " Content type: " + con.getContentType());
        InputStream is = con.getInputStream();
        im = ImageIO.read(is);
        is.close();
        return im;
    }
    /**
     * Převede dekadické číslo na hexadecimální
     * @param number
     * @return 8-místný hexadecimální řetězec
     */
    public static String toHex(int number){
        return String.format("%8s", Integer.toHexString(number)).replace(' ', '0').toUpperCase();
    }
    public static void main(String[] args) {
        /***
        try {
            BufferedImage im = getImage(new URL("http://amapy.atlas.cz/printmap.ashx?an=atlasof&sc=4000&cx=-718100&cy=-1037050&h=25&w=25"), new Proxy(Proxy.Type.HTTP,
                    new InetSocketAddress("cache-bak.pre.cz", 8080)));
            ImageIO.write(im, "png", new File("/TEMP/test.png"));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        /***/
        int d=1234567;
        System.out.println(toHex(d));
        System.out.println(toHex(-d));
    }
}
